using System;
using System.IO;
using UnityEngine;

namespace LS.Digitize
{
    public sealed class DateTimeSerializer: CompoundSerializer<DateTime>
    {
        public DateTimeSerializer() : base(typeof(DateTime),typeof(string))
        {
            
        }

        public override void WriteData(IWriter writer, DateTime obj)
        {
            ISerializer subSerializer = SubSerializer;
            if (subSerializer is PrimitiveSerializer<string> ps)
                ps.WriteData(writer, obj.ToString());
            else
                subSerializer.Write(writer, obj.ToString());
        }

        public override DateTime ReadData(IReader reader)
        {
            ISerializer subSerializer = SubSerializer;
            if (subSerializer is PrimitiveSerializer<string> ps)
                return DateTime.Parse(ps.ReadData(reader));
            else
                return DateTime.Parse((string)subSerializer.Read(reader));
            
        }
        
        public override void ReadInto(IReader reader, ref DateTime obj)
        {
            obj = ReadData(reader);
        }
    }
}